package org.grayrabbit.cms.util;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;


public class FilePathSptUtil {
	  
	private static final String SPT=File.separator;   
	public static final String URL_SPT="/";
	/**
	 * 主题根目录
	 */
	public static final String THEME_ROOT_PATH;	
	public static final String THEME_VISIT_URL;	
	/**
	 * 本地站点发布路径
	 */
	public static final String LOCAL_SITE_PUBLISH_PATH;
	/**
	 * cms 访问 contextPath
	 * 
	 */
	public static final String CMS_VISIT_CONTEXT_PATH;
	
	/**站点的访问路径*/
	public static final String SITE_VISIT_URL;
	/**
	 * 静态资源URL发布路径
	 */
	public static final String STATIC_RESOURCES_URL;
	/**
	 * 上传资源发布路径
	 */
	public static final String UPLOAD_ROOT_PATH;
	/**
	 * 静态上传资源路径
	 */
	public static final String UPLOAD_URL;
	/**
	 * 调查模板路径
	 */
	public static final String SURVEY_MODEL_URL;
	
	/**
	 * 文件索引路径
	 */
	public static final String LUCENE_INDEX;
	/**
	 * 用户密码公钥
	 */
	public static final String MEMBER_PASSWORD_KEY;
	/**
	 * 新浪appid
	 */
	public static final String SINA_APP_ID;
	/**
	 * 新浪appsecret
	 */
	public static final String SINA_APP_SECRET;
	
	/* 理事会图片rootpath*/
	public static final String COUNCIL_IMG_ROOT_PATH;
	/* 理事会图片web url*/
	public static final String COUNCIL_IMG_VISIT_URL;
	public static final String UPLOAD_CMS = "cms";
	public static final String UPLOAD_FRONT = "front";
	public static final String CONTENT_PICTURE = "picture";
	public static final String CONTENT_SOFTWARE = "software";
	public static final String CONTENT_FLASH = "flash";
	public static final String CONTENT_VIDEO = "video";
	public static final String CONTENT_OTHER = "other";
	public static final String FRONT_ESP = "esp";
	public static final String FRONT_HONOR = "honor";
	public static final String FRONT_CERTIFICATE = "certificate";
	public static final String FRONT_PRODUCT = "product";
	public static final String FRONT_ENTERPRISE = "enterprise";
	
	public static final String UPLOAD_TAX = "tax";
 
	public static final String CMS_SITE = "site";
	public static final String CMS_LOGO = "logo";
	
	
	public static final String MAIN_SITE_ID;
	public static final String LINK_NUM;
  
	/** 去掉左侧的"/"（路径分隔符）
	 * @param path
	 * @return
	 */
	public static String trimLeftSpt(String path) {
		if(path.startsWith(String.valueOf(SPT))) {
			path = path.substring(1);
		}
		
		return path;
	}
	
	/** 去掉右侧的"/"（路径分隔符）
	 * @param path
	 * @return
	 */
	public static String trimRightSpt(String path) {
		if(path.endsWith(String.valueOf(SPT))) {
			path = path.substring(0, path.length() - 1);
		}
		
		return path;
	}
	
	/** 去掉左侧 、右侧的"/"（路径分隔符）
	 * @param path
	 * @return
	 */
	public static String trimLeftAndRightSpt(String path) {
		path = trimLeftSpt(path);
		path = trimRightSpt(path);
		return path;
	}
	
	/** 去掉左侧 、右侧的"/"（路径分隔符）,给左侧加上"/"
	 * @param path
	 * @return
	 */
	public static String getNormalPath(String path) {
		path = trimLeftAndRightSpt(path);
		path = padLeftSpt(path);
		return path;
	}
	
	/** 保证 路径 左侧加上"/"（路径分隔符）
	 * @param path
	 * @return
	 */
	public static String padLeftSpt(String path) {
		if(!path.startsWith(String.valueOf(SPT))) {
			path = SPT + path;
		}		
		return path;
	}
	
	/** 给 路径 右侧加上"/"（路径分隔符）
	 * @param path
	 * @return
	 */
	public static String padRightSpt(String path) {
		if(!path.endsWith(String.valueOf(SPT))) {
			path = path + SPT;
		}		
		return path;
	}
	
	
	
	/** 给 路径 右右两侧都加上"/"（路径分隔符）
	 * @param path
	 * @return
	 */
	public static String padRightAndLeftSpt(String path) {
		path = padLeftSpt(path);
		path = padRightSpt(path);
		return path;
	}
	
	public static String urlPadRightSpt(String path){
		if(!path.endsWith(URL_SPT)) 
			path += URL_SPT;
		return path;
	}
	
	public static String urlPadHttp(String path){
		path =urlPadRightSpt(path);
		if(!path.startsWith("http://")) path = "http://"+path;
		return path;
	}
	
	
	/**
	 *  存储文件相对路径到数据库或者web路径前台展示时 转换wiondows 下的"\\"为"/"
	 * @param path
	 * @return
	 */
	public static String transferSpt(String path){
		if(StringUtils.isBlank(path)) return path;
		return path.replace("\\", URL_SPT);
	}

	static {
		Properties prop = new Properties();
		try {
			InputStream stream = FilePathSptUtil.class.getResourceAsStream("/application.properties");
			prop.load(stream);
		} catch (Exception e) {
		}
		LOCAL_SITE_PUBLISH_PATH = prop.getProperty("site.publish.path", "/");
		SITE_VISIT_URL = prop.getProperty("site.visit.url", "/");
		STATIC_RESOURCES_URL = prop.getProperty("static.resources.url", "/");
		UPLOAD_ROOT_PATH = prop.getProperty("upload.root.path","/");
		UPLOAD_URL = prop.getProperty("upload.url", "/");
		SURVEY_MODEL_URL = prop.getProperty("survey.model.url","/");
		THEME_ROOT_PATH=prop.getProperty("theme.root.path","/");
		THEME_VISIT_URL=prop.getProperty("theme.visit.url","/");
		CMS_VISIT_CONTEXT_PATH=prop.getProperty("cms.visit.context.path","/");
		COUNCIL_IMG_ROOT_PATH=prop.getProperty("council.img.root.path","/");
		COUNCIL_IMG_VISIT_URL=prop.getProperty("council.img.visit.url","/");
		MAIN_SITE_ID=prop.getProperty("main.site.id","/");
		LINK_NUM=prop.getProperty("link.num","7");
		LUCENE_INDEX=prop.getProperty("lucene.index");
		MEMBER_PASSWORD_KEY=prop.getProperty("member.password.key","will_yin@true%is#handsome&foolish");
		SINA_APP_ID = prop.getProperty("sina.appid", "3026065655");
		SINA_APP_SECRET = prop.getProperty("sina.appsecret", "a44bdf417dcf4b59e1a950f152ce7de5");
	}
   

}
